package com.CoreryBlack.controller;

import com.CoreryBlack.anno.Log;
import com.CoreryBlack.po.*;
import com.CoreryBlack.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description EmpController
 * @Author CoreryBlack
 * @Date 2024-09-20
 */

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 列表查询
     * @param  empQueryParam
     * @return
     */
    @CrossOrigin("*")//设置允许跨域访问
    @GetMapping
    public Result list(EmpQueryParam empQueryParam){
        PageBean pageBean = empService.list(empQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 新增员工
     * @param emp
     * @return
     */
    @Log
    @PostMapping
    public Result saveEmp(@RequestBody Emp emp) throws Exception{
        empService.save(emp);
        return Result.success();
    }

    @Log
    @DeleteMapping
    public Result deleteById(@RequestBody List<Integer> ids){
        empService.deleteById(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) throws Exception{
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        try {
            // 查询所有班级信息
            List<Emp> empList = empService.queryString();
            return Result.success(empList); // 返回成功结果
        } catch (Exception e) {
            return null;// 返回失败结果
        }
    }
}
